package itemControllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"lazyops_go/models"
)

type ItemController struct {
	beego.Controller
}

func (c *ItemController) IndexPrj() {
	//data_list :=models.QueryPrjAll()
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	role_id_list := c.GetSession("role_id_list").([]int)
	data_list := models.GetRolePrj(role_id_list)
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["data_list"] = data_list
	c.Data["label"] = "项目列表"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "items/prjjs.html"
	c.TplName = "items/prj.html"
}


func (c *ItemController) SearchPrj() {
	key := c.Ctx.Input.Param(":key")
	data_list :=models.QueryPrjSearch(key)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	c.Data["key"] = key
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["data_list"] = data_list
	c.Data["label"] = "项目列表"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "items/prjjs.html"
	c.TplName = "items/prj.html"
}

func (c *ItemController) CreatePrj() {
	prj_name := c.GetString("prj_name")
	obj := models.ItemPrj{}
	obj.PrjName = prj_name
	err :=models.AddPrj(obj)
	if err != nil{
		c.Data["json"] = err
	}else{
		c.Data["json"] = "添加成功"
	}
	c.ServeJSON()
}

func (c *ItemController)  GetIdPrj() {
	id,_ := c.GetInt("id",0)
	data := models.QueryPrjOne(id)
	c.Data["json"] = data
	c.ServeJSON()
}

func (c *ItemController) EditPrj()  {
	id,_ := c.GetInt("id",0)
	prj_name := c.GetString("prj_name")
	obj := models.ItemPrj{}
	obj.PrjName = prj_name
	err := models.UpdatePrj(id,obj)
	if err != nil{
		fmt.Println(id,err)
		c.Data["json"] = err
	}else{
		c.Data["json"] = "更新成功"
	}
	c.ServeJSON()
}

func (c *ItemController) DelPrj() {
	id,_ := c.GetInt("id",0)
	err := models.DeletePrj(id)
	if err != nil {
		c.Data["json"] = err
	}else{
		c.Data["json"] = "已删除"
	}
	c.ServeJSON()
}
